package com.mmlc.meimei.lichengkongjian.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.CategoryListResponseBean;

import java.util.List;

public class CustomGridView extends LinearLayout {
    public boolean leftShow;
    public int right_height;
    public int right_widht;
    public boolean isResetRightViewWidth = true;
    public int xCount = 4;
    public int yCount;
    public List<CategoryListResponseBean.DataBean.ListBean> data;

    public CustomGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                getViewTreeObserver().removeOnPreDrawListener(this);
                initView();
                return false;
            }
        });
    }

    private void initView() {
        setOrientation(VERTICAL);
        if ((data.size() % xCount) == 0) {//能整除
            yCount = data.size() / xCount;
        } else {//不能整除
            yCount = data.size() / xCount + 1;
        }

        for (int j = 0; j < yCount; j++) {
            LinearLayout ll = new LinearLayout(getContext());
            LayoutParams llParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
            llParams.weight = 1;

            ll.setBackgroundColor(getResources().getColor(R.color.point_grey));
            ll.setOrientation(LinearLayout.HORIZONTAL);
            ll.setLayoutParams(llParams);
            this.addView(ll);
            for (int i = 0; i < xCount && (j * xCount + i) < data.size(); i++) {
                LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.weight = 1;
                TextView textView = new TextView(getContext());
                textView.setText(data.get(j * xCount + i).getName());
                textView.setTextColor(getResources().getColor(R.color.text_black));
                textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
                textView.setLayoutParams(params);

                ll.addView(textView);
            }
        }
    }

    public void setData(List<CategoryListResponseBean.DataBean.ListBean> data) {
        this.data = data;
    }
}
